/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2025. All rights reserved.
 */

import { Level, levels } from 'log4js';

export class CoreParameter {
  private _properties: CoreProperties = {};
  private _extParams: Record<string, string> = {};
  private _startParams: CoreStartParam = { ...defaultStartParam };
  private _workspaceDir = '';

  get properties(): CoreProperties {
    return this._properties;
  }

  set properties(value: CoreProperties) {
    this._properties = value;
  }

  get extParams(): Record<string, string> {
    return this._extParams;
  }

  set extParams(value: Record<string, string>) {
    this._extParams = value;
  }

  get startParams(): CoreStartParam {
    return this._startParams;
  }

  get workspaceDir(): string {
    return this._workspaceDir;
  }

  set workspaceDir(value: string) {
    this._workspaceDir = value;
  }

  clean() {
    this._properties = {};
    this._extParams = {};
    this._startParams = { ...defaultStartParam };
    this._workspaceDir = '';
  }
}

export enum AnalyzeMode {
  NORMAL,
  ADVANCED,
  FALSE,
  TRACE,
}

export enum OptimizationStrategy {
  PERFORMANCE = 'performance',
  MEMORY = 'memory',
}

export const AnalyzeModeMap = new Map<string | boolean, AnalyzeMode>([
  ['default', AnalyzeMode.NORMAL],
  ['verbose', AnalyzeMode.ADVANCED],
  [false, AnalyzeMode.FALSE],
  ['false', AnalyzeMode.FALSE],
  ['normal', AnalyzeMode.NORMAL],
  ['advanced', AnalyzeMode.ADVANCED],
  ['ultrafine', AnalyzeMode.TRACE],
]);

export const OldAnalyzeModeMap = new Map<string | boolean, string>([
  ['default', 'normal'],
  ['verbose', 'advanced'],
]);

export const AnalyzeModeKeyMap = new Map<number, string | boolean>([
  [AnalyzeMode.NORMAL, 'normal'],
  [AnalyzeMode.ADVANCED, 'advanced'],
  [AnalyzeMode.TRACE, 'ultrafine'],
  [AnalyzeMode.FALSE, false],
]);

export const LogLevelMap = new Map<string, Level>([
  ['info', levels.INFO],
  ['debug"', levels.DEBUG],
  ['warn"', levels.WARN],
  ['error"', levels.ERROR],
]);

export const defaultStartParam: CoreStartParam = {
  hvigorfileTypeCheck: false,
  parallelExecution: true,
  incrementalExecution: true,
  printStackTrace: false,
  daemon: true,
  analyze: AnalyzeMode.NORMAL,
  logLevel: levels.INFO,
  optimizationStrategy: OptimizationStrategy.MEMORY,
  hotCompile: false,
  hotReloadBuild: false,
};

export const defaultProperties: CoreProperties = {
  enableSignTask: true,
  skipNativeIncremental: false,
  'hvigor.keepDependency': true,
};

export interface CoreStartParam {
  hvigorfileTypeCheck: boolean;
  parallelExecution: boolean;
  incrementalExecution: boolean;
  printStackTrace: boolean;
  daemon: boolean;
  analyze: AnalyzeMode;
  logLevel: Level;
  optimizationStrategy: string;
  hotCompile?: boolean;
  hotReloadBuild?: boolean;
}

export interface CoreProperties {
  ohosArkCompileMaxSize?: number;
  hvigorPoolMaxSize?: number;
  hvigorPoolMaxCoreSize?: number;
  hvigorPoolCacheCapacity?: number;
  hvigorPoolCacheTtl?: number;
  enableSignTask?: boolean;
  skipNativeIncremental?: boolean;
  'hvigor.analyzeHtml'?: boolean;
  'ohos.compile.lib.entryfile'?: boolean;
  'ohos.sign.har'?: boolean;
  [key: string]: ParamValue;
  'ohos.arkCompile.sourceMapDir'?: string;
  hvigorMemoryThreshold?: number;
  'ohos.processLib.optimization'?: boolean;
  'ohos.obfuscationRules.optimization'?: boolean;
  'ohos.byteCodeHar.integratedOptimization'?: boolean;
  enableOverridesDependencyMap?: boolean;
}

export type ParamValue = string | boolean | number | undefined;
export const coreParameter = new CoreParameter();
